<% layout('/layout/default.html', {title: '用户管理', libs: ['dataGrid','icheck']}){ %>
<div class="main-content">
	<div class="card-header">
		<i class="fa icon-user"></i> 用户管理
		<div class="header-tools float-right">
			<a href="#" class="btn btn-default btn-sm" id="btnSearch" title="查询"><i class="fa fa-filter"></i> 查询</a>
<!--			<a href="#" class="btn btn-default btn-sm"  id="btnRefreshTree" title="刷新"><i class="fa fa-refresh"></i> 刷新</a>-->
			<% if(hasPermi('sys:user:edit')){ %>
			<a href="/sys/user/form" class="btn btn-default btn-sm btnTool" title="新增用户"><i class="fa fa-plus"></i> 新增</a>
			<div class="btn-group">
				<a href="javascript:" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">
					<i class="fa fa-navicon"></i> <span class="caret"></span>
				</a>
				<div class="dropdown-menu dropdown-menu-right">
					<a href="${ctx}/sys/user/export" class="dropdown-item" id="btnExport"><i class="fas fa-arrow-alt-circle-up"></i> 导出</a>
					<div class="dropdown-divider"></div>
					<a href="javascript:" class="dropdown-item"  id="btnImport"><i class="fas fa-arrow-alt-circle-down"></i> 导入</a>
				</div>
			</div>
			<% } %>
		</div>
	</div>
	<div class="container-fluid">
		<form id="searchForm" action="${ctx}/sys/user/listData" class="form-inline d-none" >
			<input id="officeId" name="office.id" type="hidden" value=""/>
			<div class="form-group">
				<label class="control-label">登录名：</label>
				<div class="control-inline">
					<#form:input path="loginName" maxlength="100" class="form-control form-control-sm"/>
				</div>
			</div>
			<div class="form-group">
				<label class="control-label">邮箱：</label>
				<div class="control-inline">
					<#form:input path="email" maxlength="300" class="form-control  form-control-sm"/>
				</div>
			</div>
			<div class="form-group">
				<label class="control-label">手机：</label>
				<div class="control-inline">
					<#form:input path="mobile" maxlength="100" class="form-control form-control-sm"/>
				</div>
			</div>
			<div class="form-group">
				<label class="control-label">电话：</label>
				<div class="control-inline">
					<#form:input path="phone" maxlength="100" class="form-control form-control-sm"/>
				</div>
			</div>
			<div class="form-row"></div>
			<div class="form-group">
				<label class="control-label">用户名：</label>
				<div class="control-inline">
					<#form:input path="name" maxlength="100" class="form-control form-control-sm"/>
				</div>
			</div>
			<div class="form-group">
				<label class="control-label">状态：</label>
				<div class="control-inline width-90">
					<#form:select path="loginFlag" name="label" value="type" data="getDictList:sys_user_status" class="form-control select2bs4" />
				</div>
			</div>
			<div class="form-group">
				<button type="submit" class="btn btn-success btn-sm ">查询</button>
				<button id="reset" type="reset" class="btn btn-default btn-sm">重置</button>
			</div>
		</form>
		<table id="dataGrid"></table>
		<div id="dataGridPage"></div>
	</div>
</div>
<script type="text/javascript">

	$('#reset').click(function(){
		$('#loginFlag').val(null).trigger("change");
	});

	$('#btnImport').click(function(){
		js.layer.open({
			type: 1,
			area: ['400px'],
			title: '导入用户数据',
			resize: false,
			scrollbar: true,
			content: js.formatHtml('importTpl'),
			btn: ['<i class="fa fa-check"></i> 导入',
				'<i class="fa fa-remove"></i> 取消'],
			yes: function(index, layero){
				console.log(layero.find('#file').val());
				var form = {
					inputForm: layero.find('#inputForm'),
					file: layero.find('#file').val()
				};
				if (form.file == '' || (!js.endWith(form.file, '.xls') && !js.endWith(form.file, '.xlsx'))){
					js.showMessage("文件不正确，请选择后缀为 “xls”或“xlsx”的文件。", null, 'warning');
					return false;
				}
				js.ajaxSubmitForm(form.inputForm, function(data){
					console.log(data);
					js.layer.closeAll();
					js.showMessage(data.message);
					page();
				});
				return true;
			},
			btn2: function(index, layero){
			}
		});
	});
</script>
<script id="importTpl" type="text">//<!--
<form id="inputForm" action="${ctx}/sys/user/import" method="post" enctype="multipart/form-data"
	class="form-horizontal mt20 mb10" style="overflow:auto;max-height:200px;">
	<div class="row">
		<div class="col-xs-12 col-xs-offset-1">
			<input type="file" id="file" name="file" class="form-file"/>
			<div class="mt10 pt5">
				<a href="${ctx}/sys/user/import/template" class="btn btn-default btn-xs"><i class="fa fa-file-excel-o"></i> 下载模板</a>
			</div>
			<font color="red" class="pull-left mt10">
				提示：仅允许导入“xls”或“xlsx”格式文件！
			</font>
		</div>
	</div>
</form>
//--></script>
<% } %>
<script>
	$('#dataGrid').dataGrid({
		searchForm: $("#searchForm"),
		columnModel: [
			{header:'登录名', name:'loginName', index:'a.login_name', width:100, align:"center"},
			{header:'用户名', name:'name', index:'a.name', width:100, align:"center"},
			{header:'归属公司', name:'company.name', index:'company.id', width:100, align:"center",sortable:false},
			{header:'归属机构', name:'office.name', index:'office.id', width:100, align:"center",sortable:false},
			{header:'电子邮箱', name:'email', index:'a.email', width:100, align:"center"},
			{header:'手机号码', name:'mobile', index:'a.mobile', width:100, align:"center"},
			{header:'状态', name:'loginFlag', index:'a.login_flag', width:80, align:"center", formatter: function(val, obj, row, act){
					return js.transDictLabel(${@DictUtils.getDictListJson('sys_user_status')}, val, '未知');
				}},
			{header:'操作', name:'actions', width:150, sortable:false,align:"center", title:false, formatter: function(val, obj, row, act){
					var actions = [];
					var icon = row.loginFlag==1?'title="停用用户" data-confirm="确认要停用该用户吗?"><i class="fa fa-ban':'title="启用用户" data-confirm="确认要启该用户吗?"><i class="fa fa-check-square';
				<% if(hasPermi('sys:user:edit')){ %>
						actions.push('<a href="/sys/user/form?id='+row.id+'" class="btnList" title="编辑用户"><i class="fas fa-pencil-alt"></i></a>');
						actions.push('<a href="${ctx}/sys/user/changeStatus?id='+row.id+'&loginFlag='+row.loginFlag+'" class="btnList"'+icon+'"></i></a>');
						actions.push('<a href="${ctx}/sys/user/resetPass?id='+row.id+'&loginName='+row.loginName+'" class="btnList" title="重置密码" data-confirm="确认要重置为默认密码吗?"><i class="fa fa-reply-all"></i></a>');
						actions.push('<a href="${ctx}/sys/user/delete?id='+row.id+'" class="btnList" title="删除用户" data-confirm="确认要删除该用户吗?"><i class="far fa-trash-alt"></i></a>');
					<% } %>
					return actions.join('');
				}}
		],
		// 加载成功后执行事件
		ajaxSuccess: function(data){
			var ids = $("#dataGrid").jqGrid("getDataIDs");//获取所有行的id
			var rowDatas = $("#dataGrid").jqGrid("getRowData");//获取所有行的数据
			for(var i=0; i<rowDatas.length; i++){
				var rowData = rowDatas[i];
				if(rowData.loginFlag == '停用'){//账号停用
					$("#"+ids[i]).addClass("ui-state-active");
					// $("#"+ids[i]).find("td").addClass("SelectBG");
				}
			}
		}
	});
</script>
